package com.jack.online.study.controller.cms.api;


import com.jack.online.study.common.base.result.R;
import com.jack.online.study.model.cms.dao.Ad;
import com.jack.online.study.service.cms.AdService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin //解决跨域问题
//@Api(description = "广告推荐")
@RestController
@RequestMapping("/api/cms/ad")
public class ApiAdController {

    private Map<String,Object> redis = new HashMap<>();
    @Resource
    private AdService adService;

//    @Resource
//    private RedisTemplate redisTemplate;

//    @ApiOperation("根据推荐位id显示广告推荐")
    @GetMapping("list/{adTypeId}")
    public R listByAdTypeId(
//            @ApiParam(value = "推荐位id", required = true)
                                @PathVariable String adTypeId) {

        List<Ad> ads = adService.selectByAdTypeId(adTypeId);
        return R.ok().data("items", ads);
    }
/*
*
* redis测试
* */
    @PostMapping("save-test")
    public R saveAd(@RequestBody Ad ad){
        //redisTemplate.opsForValue().set("ad1", ad);
        redis.put("ad",ad);
//        redisTemplate.opsForValue().set("index::ad", ad);
        return R.ok();
    }

    @GetMapping("get-test/{key}")
    public R getAd(@PathVariable String key){
//        Ad ad = (Ad)redisTemplate.opsForValue().get(key);
        Object o = redis.get(key);
        return R.ok().data("ad", o);
    }

    @DeleteMapping("remove-test/{key}")
    public R removeAd(@PathVariable String key){
//        Boolean delete = redisTemplate.delete(key);
//        System.out.println(delete);//是否删除成功
//        Boolean hasKey = redisTemplate.hasKey(key);
//        System.out.println(hasKey);//key是否存在
        return R.ok();
    }


}